﻿using System;
using System.Windows;

namespace Silverlight.InputCtrl
{
    /// <summary>
    /// Abstract Base Validator
    /// </summary>
    public abstract class ValidatorBase : DependencyObject, IBaseValidator
    {
        private FrameworkElement _elemValidate;
        private Exception _ExceptionValidator = null;
        private bool _isvalid = false;

        #region IBaseValidator Members

        /// <summary>
        /// See Ivalidate
        /// </summary>
        public bool Enabled
        {
            get { return (bool)GetValue(EnabledProperty); }
            set { SetValue(EnabledProperty, value); }
        }

        public static readonly DependencyProperty EnabledProperty =
            DependencyProperty.Register("Enabled", typeof(bool), typeof(ValidatorBase), new PropertyMetadata(true));

        /// <summary>
        /// See Ivalidate
        /// </summary>
        public virtual void InitValidator(FrameworkElement element, string NameProperty)
        {
            PropertyToValidate = NameProperty;
            if (element != null)
            {
                ExceptionValidator = null;
                if (NameProperty == "")
                {
                    if (ValidatorCommon.GetTypeElement(element) == DefaultTypeToValidator.None)
                    {
                        ExceptionValidator = new Exception(Resource.ValidatorBase_ElementInvalidType);
                    }
                }
                ElementValidator = element;
                IsValidResult = false;
            }
            else
            {
                ElementValidator = null;
            }
        }

        /// <summary>
        /// See Ivalidate
        /// </summary>
        public string PropertyToValidate 
        {
            get { return (string)GetValue(PropertyToValidateProperty); }
            set { SetValue(PropertyToValidateProperty, value); }
        }

        public static readonly DependencyProperty PropertyToValidateProperty =
            DependencyProperty.Register("PropertyToValidate", typeof(string), typeof(ValidatorBase), new PropertyMetadata(""));

        public FrameworkElement ElementValidator
        {
            get { return _elemValidate; }
            internal set { _elemValidate = value; }
        }

        /// <summary>
        /// See Ivalidate
        /// </summary>
        public string ErrorMessage
        {
            get { return (string)GetValue(ErrorMessageProperty); }
            set { SetValue(ErrorMessageProperty, value); }
        }

        public static readonly DependencyProperty ErrorMessageProperty =
            DependencyProperty.Register("ErrorMessageProperty", typeof(string), typeof(ValidatorBase), new PropertyMetadata(""));

        /// <summary>
        /// See Ivalidate
        /// </summary>
        /// <remarks>
        /// public Read only 
        /// </remarks>
        public bool IsValidResult
        {
            get { return _isvalid; }
            internal set { _isvalid = value; }
        }

        /// <summary>
        /// See Ivalidate
        /// </summary>
        /// <remarks>
        /// public Read only 
        /// </remarks>
        public Exception ExceptionValidator
        {
            get { return _ExceptionValidator; }
            internal set { _ExceptionValidator = value; }
        }

        /// <summary>
        /// See Ivalidate
        /// </summary>
        public abstract bool Validate();

        /// <summary>
        /// See Ivalidate
        /// </summary>
        public void Reset()
        {
            _isvalid = false;
        }

        /// <summary>
        /// See Ivalidate
        /// </summary>
        public string Group
        {
            get { return (string)GetValue(GroupProperty); }
            set { SetValue(GroupProperty, value); }
        }

        public static readonly DependencyProperty GroupProperty =
            DependencyProperty.Register("GroupProperty", typeof(string), typeof(ValidatorBase), new PropertyMetadata(""));


        #endregion

    }
}
